package com.hyxt.DTO.process;

import com.hyxt.DTO.CommonMessage;
import com.hyxt.DTO.CommonMessageHeader;
import com.hyxt.DTO.T809_MessageHeader;
import com.hyxt.superviseservice.CommonMessageBody;
import com.hyxt.superviseservice.IMessage;
import com.hyxt.superviseservice.IMessageBody;
import com.hyxt.superviseservice.messageBody.T809_0x1001;
import com.hyxt.utils.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @Project: CNPC_VMS
 * @Title: T809_0x1001_Process
 * @Description: T809_0x1001_Process
 * @author: denghl
 * @date: 2014年4月24日 下午4:38:32
 * @company: Beijing Huayou Information andCommunication Technology Co.,Ltd
 * @Copyright: Copyright (c) 2014
 * @version V2.0
 */

public class T809_0x1001_Process<B extends IMessageBody> extends T809_Process<B> {
	private static Log log = LogFactory.getLog(T809_0x1001_Process.class);

	
	
	public static void main(String[] args) {
		
		byte[] bytes = BytesUtil.toStringHex("5b"
				+ "00000048"
				+ "00000000"
				+ "1001"
				+ "00989683010000000000000000c65e014731303030303030303131332e33312e32382e31313100000000000000000000000000000000000000271960845d");
		         bytes = BytesUtil.toStringHex("5b0000004800000fe1100101421cf101000000000000000098968031303030303030303230332e37392e302e3132000000000000000000000000000000000000000000046ed3565d");
		//log.debug(new String(bytes));
		try {
			IMessage mes = new T809_ProtocolAnalysis(new int[]{10000000,20000000,30000000}).unpack(bytes);
			log.debug(mes);
		} catch (ProtocolCheckExeption e) {
			e.printStackTrace();
		} catch (ProtocolEscapeExeption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @Description 获取数据体部分
	 */
	@Override
	protected CommonMessageBody getBody(CommonMessageHeader header, byte[] data) {
		
		
		//
		T809_0x1001 body = new T809_0x1001();
		// 截取用户名
		byte[] userID = BytesUtil.cutBytes(0, 4, data);
		// 截取密码
		byte[] PASSWORD = BytesUtil.cutBytes(4, 8, data);
		// 截取下级ip
		byte[] DOWN_LINK_IP = BytesUtil.cutBytes(12, 32, data);
		// 截取下级端口
		byte[] DOWN_LINK_PORT = BytesUtil.getWord(44, data);
		// 设置用户名
		body.setUserID(BytesUtil.parseBytesToInt(userID));
		try {
			// 设置密码
			body.setPASSWORD(new String(PASSWORD, "US-ASCII"));
		} catch (UnsupportedEncodingException e) {
			log.warn("", e);
		}
		try {
			// 设置下级ip
			body.setDOWN_LINK_IP(new String(DOWN_LINK_IP, "US-ASCII"));
		} catch (UnsupportedEncodingException e) {
			log.warn("", e);
		}
		// 设置端口
		body.setDOWN_LINK_PORT(BytesUtil.parseBytesToShort(DOWN_LINK_PORT));
		return body;
	}

	/**
	 * @Description 重写打包方法
	 */
	@Override
	public byte[] packData(CommonMessage<B> message) {

		T809_0x1001 body1001 = (T809_0x1001) message.getMessageBody();
		// 此数据体数据长度为27,数据长度(包括头标识、数据头、数据体、CRC校验码和尾标识)
		// 总共68字节
		byte[] head = new byte[22];
		byte[] datalength = BytesUtil.int2bytes4(72);
		datalength = BytesUtil.getBigDWord(0, datalength);
		System.arraycopy(datalength, 0, head, 0, datalength.length);

		// 报文序列号*占4字节
		byte[] MSG_SN = BytesUtil.int2bytes4(((T809_MessageHeader) message.getMessageHeader()).getSn());
		MSG_SN = BytesUtil.getBigDWord(0, MSG_SN);
		System.arraycopy(MSG_SN, 0, head, 4, MSG_SN.length);
		// 业务数据类型,占2字节
		byte[] MSG_ID = BytesUtil.int2bytes2(Integer.parseInt((((T809_MessageHeader) message.getMessageHeader()).getMessageID()).toString()));
		head[8] = 0x10;
		head[9] = 0x01;
		// 下级平台接入码，上级平台给下级平台分配唯一标识码。占4字节
		byte[] MSG_GNSSCENTERID = BytesUtil.int2bytes4(((T809_MessageHeader) message.getMessageHeader()).getGnsscentrid());
		MSG_GNSSCENTERID = BytesUtil.getBigDWord(0, MSG_GNSSCENTERID);
		System.arraycopy(MSG_GNSSCENTERID, 0, head, 10, MSG_GNSSCENTERID.length);
		// 协议版本好标识，上下级平台之间采用的标准协议版编号；长度为3个字节来表示，0x01 0x02 0x0F
		// 标识的版本号是v1.2.15，以此类推。
		byte[] VERSION_FLAG = ((T809_MessageHeader) message.getMessageHeader()).getVersonFlag();
		System.arraycopy(VERSION_FLAG, 0, head, 14, VERSION_FLAG.length);
		// 报文加密标识位b: 0表示报文不加密，1表示报文加密。占1字节
		// byte ENCRYPT_FLAG =
		// ((T809_MessageHeader)message.getMessageHeader()).getEncryptFlag();
		head[17] = 0x00;
		// 数据加密的密匙，长度为4个字节。
		byte[] ENCRYPT_KEY = BytesUtil.int2bytes4(((T809_MessageHeader) message.getMessageHeader()).getEncryptKey());

		System.arraycopy(ENCRYPT_KEY, 0, head, 18, 4);

		byte[] body = new byte[46];
		// 用户名
		byte[] userID = BytesUtil.int2bytes4(body1001.getUserID());
		userID = BytesUtil.getBigDWord(0, userID);

		System.arraycopy(userID, 0, body, 0, userID.length);
		// 密码
		byte[] passwd = body1001.getPASSWORD().getBytes();

		System.arraycopy(passwd, 0, body, 4, passwd.length);
		// ip
		byte[] LinkIP = body1001.getDOWN_LINK_IP().getBytes();

		System.arraycopy(LinkIP, 0, body, 12, LinkIP.length);
		// 端口
		byte[] port = BytesUtil.int2bytes2(body1001.getDOWN_LINK_PORT());

		body[44] = port[0];
		body[45] = port[1];
		// 对数据头到校验码前的CRC值,占两个字节

		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] mes = null;
		try {
			bos.write(0x00);
			// 将数据头与数据体写入流中
			bos.write(head);
			bos.write(body);
			bos.write(0x00);
			bos.write(0x00);
			bos.write(0x00);
			// 将数据流转成byte数组
			byte[] bosBytes = bos.toByteArray();
			// 计算crc校验码
			byte[] CRCCode = T809_Util.check(bosBytes);
			bosBytes[bosBytes.length - 3] = CRCCode[0];
			bosBytes[bosBytes.length - 2] = CRCCode[1];
			// 将数据转义
			mes = T809_Util.escapeData(bosBytes);
			mes[0] = 0x5b;
			// 加入数据头与数据尾
			mes[mes.length - 1] = 0x5d;
		} catch (IOException e) {
			log.warn("", e);
		} finally {
			// 如果数据流不为空，关闭流
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					log.warn("", e);
				}
			}
		}

		log.debug("下级平台登陆请求" + BytesUtil.bytesToHexString(mes));
		return mes;

	}

}
